/**
 * 
 */
package server;

import java.util.Iterator;
import java.util.Map;

import ocsf.server.ConnectionToClient;

/**
 * @author Vodi
 *
 */
public class ServerMaintenance extends Thread {

	/**
	 * constant that holds the amount of milliseconds in one minute
	 */
	private final int minuteInMilliSec = 60000;
	
	private ServerHandler srvhnd;
	
	private Map<Long, ConnectionToClient> connected_users;
	
	public ServerMaintenance(ServerHandler srvhnd) {
		this.srvhnd = srvhnd;
	}
	
	public void run() {
		connected_users = srvhnd.getConnectedUsers();		// get a reference of the map
			
		while(true) {
			Iterator<Map.Entry<Long, ConnectionToClient>> iter = connected_users.entrySet().iterator();
					
			while(iter.hasNext()) {
				Map.Entry<Long, ConnectionToClient> entry = iter.next();
				if( !entry.getValue().isAlive()) {
					System.out.println("User ID: " + entry.getKey() + " is no longer connected. Removing...");
					iter.remove();
				}
				else {
					// System.out.println("ID: " + entry.getKey() + " connected from - " + entry.getValue());
				}
			}

			// put thread to sleep for 1 minute
			try {
				sleep(minuteInMilliSec/3);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}

}
